<?php
//REQUIRE_ONCE MODULE_PATH.'/area/config/area_config.php';

class Action
{
    private $per_page = 100;

    public function __construct() {
//        echo '123';
    }

    /**
     * 测试获取地区列表
     */
    public function test()
    {
        $res = _widget('area')->get_area_list();
        $res2 = _widget('area')->get_area_tree(10);
        echo '<pre>';
        print_r($res);
        print_r($res2);
        echo '</pre>';
    }
    
    
    /***
     * 显示地区分类的列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());
        
        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['area']) && $search_filter['area']) {
            $filter['`area` LIKE '] = $search_filter['area'].'%';
        }
        
        if (isset($search_filter['area_code']) && $search_filter['area_code']) {
        	$filter['area_code'] = $search_filter['area_code'];
        }
        
        $filter['status'] = $status;
        
        //分页
        $area_count = _model('region')->getTotal($filter);
        
        $area_list = array();
        
        $area_list = get_data_list('region', $filter, ' ORDER BY `area_code` ASC ', $page, $this->per_page);
        
        Response::assign('area_list', $area_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        
        Response::display('admin/area_list.html');
    }
    

    /**
     * 添加分类
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
    * ajax_get_provinces
    */
    public function ajax_get_provinces()
    {
        $filter = array();
        $filter['status'] = 1;
        $filter['depth'] = 1;
        $province_list = _model('region')->getList($filter);
        echo json_encode($province_list);
    }

    /**
    * ajax_get_citys
    */
    public function ajax_get_citys()
    {
        $province_code = Request::getParam('province_code');

        $pro_info = _model('region')->read(array('area_code' => $province_code));

        $filter = array();
        $filter['parent_id'] = $pro_info['area_code'];
        $filter['depth'] = 2;
        $filter['status'] = 1;

        $city_list = _model('region')->getList($filter);

        echo json_encode($city_list);
    }

    /**
    * ajax_get_countys
    */
    public function ajax_get_countys()
    {
//        $province_code = Request::getParam('province_code', '');
        $city_code = Request::getParam('city_code', '');

//        $pro_info = _model('region')->read(array('area_code' => $province_code));

//        $city_filter = array();
//        $city_filter['depth'] = 2;
//        $city_filter['status'] = 1;
//        $filter['parent_id'] = $pro_info['id'];
//        $city_filter['area_code'] = $city_code;
//
//        $city_info = _model('region')->read($city_filter);


        $county_filter = array();
        $county_filter['depth'] = 3;
        $county_filter['status'] = 1;
        $county_filter['parent_id'] = $city_code;
       
        $county_list = _model('region')->getList($county_filter);

        echo json_encode($county_list);
    }

    /**
     * 编辑分类
     */
    public function edit()
    {
        $id       = Request::getParam('id', 0);

        $area_info = array();
        $area_info['depth'] = 1;

        $area_info = _uri('region', $id);

        $area_list = array();
        //获取单条树的数据
        if ($area_info['depth'] == 3) {
            $area_list['county'] = $area_info['area'];
        	//查询市级信息
        	$city_info = _uri('region', $area_info['parent_id']);
        	$area_list['city'] = $city_info['area'];
        	//获取省级信息
        	$province_info = _uri('region', $city_info['parent_id']);
        	$area_list['province'] = $province_info['area'];

        } elseif ($area_info['depth'] == 2) {
            $area_list['city'] = $area_info['area'];
            //获取省级信息
            $province_info = _uri('region', $area_info['parent_id']);
            $area_list['province'] = $province_info['area'];
        } elseif ($area_info['depth'] == 1) {
            $area_list['province'] = $area_info['area'];
        }
            
        Response::assign('area', $area_list);
        Response::display('admin/add.html');
    }


    /**
     * 保存
     */
    public function save()
    {
//        $province  = Request::getParam('province', '');
//        $city      = Request::getParam('city', '');
//        $county    = Request::getParam('county', '');
//        $area_code = Request::getParam('area_code');
//        //用于判断是否需要执行插入操作
//        $is_need_insert = false;
//
//        $filter = array();
//        $filter['status'] = 1;
//
//        //判断行政区划代码是否存在
//        $code_info = _model('region')->getList(array('area_code'=>$area_code));
//        if ($code_info || !$area_code) {
//        	Response::redirect(AnUrl('area/admin'));
//        }
//
//        $province_info = array();
//        $city_info     = array();
//        $county_info   = array();
//
//        //先判断传过来的值在数据库中是否存在
//        if ($county) {
//            //区县条件过滤
//            $county_filter = array();
//            $county_filter['depth'] = 3;
//            $county_filter['area']  = $county;
//            $county_info = _model('region')->read($county_filter);
//
//            if (!$county_info) {
//                // 如果没有则执行插入
//                $is_need_insert = 3;
//            }
//
//        } elseif ($city) {
//            //城市过滤
//            $city_filter = array();
//            $city_filter['depth'] = 2;
//            $city_filter['area']  = $city;
//            $city_info = _model('region')->read($city_filter);
//
//            if (!$city_info) {
//                // 如果城市不存在则执行插入
//                $is_need_insert = 2;
//            }
//
//        } elseif ($province) {
//            //省份过滤
//            $province_info = array();
//            $province_info['depth']  = 1;
//            $province_filter['area'] = $province;
//            $province_info = _model('region')->read($province_filter);
//
//            if (!$province_info) {
//                // 如果省份不存在则执行插入
//                $is_need_insert = 1;
//            }
//        }
//
//        //如果需要插入则执行插入
//        if ($is_need_insert) {
//            switch ($is_need_insert) {
//                //只有省份,且需要插入
//                case 1:
//                    //创建数据
//                    $province_info['area']      = $province;
//                    $province_info['parent_id'] = 0;
//                    $province_info['depth']     = 1;
//                    $province_info['area_code'] = $area_code;
//                    _model('region')->create($province_info);
//
//                    break;
//
//                //有城市和省份且需要插入
//                case 2:
//                    //判断省份是否存在
//                    $province_info = _model('region')->read(array('area'=>$province, 'depth'=>1));
//                    if ($province_info) {
//                    	//只需要插入城市即可
//                    	$city_info['area'] = $city;
//                    	$city_info['parent_id'] = $province_info['parent_id'];
//                    	$city_info['depth'] = 2;
//                    	_model('region')->create($city_info);
//                        ;
//                    } else {
//                        //省份不存在则需要插入省份
//                        $province_info['area'] = $province;
//                        $province_info['parent_id'] = 0;
//                        $province_info['depth'] = 1;
//                        $province_info['area_code'] = strval( floor($area_code/10000) * 10000 );
//                        $province_id = _model('region')->create($province_info);
//
//                        $city_info['area']      = $city;
//                    	$city_info['parent_id'] = $province_id;
//                    	$city_info['depth']     = 2;
//                    	$city_info['area_code'] = $area_code;
//                    	_model('region')->create($city_info);
//                    }
//
//                    break;
//
//                //省市区都需要添加
//                case 3:
//
//             //判断省份是否存在
//                    $province_info = _model('region')->read(array('area'=>$province, 'depth'=>1));
//                    if ($province_info) {
//                        //判断城市是否存在
//                        $city_info = _model('region')->read(array('area'=>$city, 'depth'=>2));
//                        if ($city_info) {
//                            //如果城市存在只需要插入区县
//                            $county_info['depth']     = 3;
//                            $county_info['area']      = $county;
//                            $county_info['parent_id'] = $city_id;
//                            $county_info['area_code'] = $area_code;
//                            _model('region')->create($county_info);
//                        } else {
//                            //插入城市
//                            $city_info['depth']     = 2;
//                            $city_info['area']      = $city;
//                            $city_info['parent_id'] = $province_info['parent_id'];
//                            $city_info['area_code'] = strval( floor($area_code/100) * 100 );
//                            $city_id = _model('region')->create($city_info);
//
//                            //插入区县
//                            $county_info['depth']     = 3;
//                            $county_info['area']      = $county;
//                            $county_info['parent_id'] = $city_id;
//                            $county_info['area_code'] = $area_code;
//                            _model('region')->create($county_info);
//                        }
//
//                    } else {
//                        //省份不存在则需要插入省份
//                        $province_info['area']      = $province;
//                        $province_info['parent_id'] = 0;
//                        $province_info['depth']     = 1;
//                        $province_info['area_code'] = strval( floor($area_code/10000) * 10000 );
//                        $province_id = _model('region')->create($province_info);
//
//                        //插入城市
//                        $city_info['area']      = $city;
//                    	$city_info['parent_id'] = $province_id;
//                    	$city_info['depth']     = 2;
//                    	$city_info['area_code'] = strval( floor($area_code/100) * 100 );
//                    	$city_id = _model('region')->create($city_info);
//
//                    	//插入区县
//                    	$county_info['area']      = $county;
//                    	$county_info['parent_id'] = $city_id;
//                    	$county_info['depth']     = 3;
//                    	$county_info['area_code'] = $area_code;
//                    	_model('region')->create($county_info);
//                    }
//                    break;
//                default:
//                    # code...
//                    break;
//            }
//        }

        Response::redirect(AnUrl('area/admin'));
    }


    /**
    * 伪删除
    */
    public function delete()
    {
//        $id = Request::getParam('id');
//
//
//        if (!$id) {
//            return '请选择要删除的内容';
//        }
//
//        $ids = explode(',', trim($id, ','));
//        foreach ($ids as $k=>$v) {
//            if (!$v) {
//                continue;
//            }
//            $info = _uri('region', $v);
//            // 查询是否含有子分类
//            $children_info = _uri('region', array('`lft` > '=>$info['lft'], '`rgt` < '=>$info['rgt']));
//            if ($children_info) {
//                return '请先删除子分类';
//            }
//            _model('region')->update($v, array('status'=>0));
//
//        }

        return 'ok';
    }

    /**
    * 恢复
    */
    public function change_status()
    {
//        $id     = Request::getParam('id', 0);
//        $status = Request::getParam('status', 1);
//        _model('region')->update($id, array('status'=>1));
//
//        Response::assign('status', 1);
        Response::redirect('area/admin');
    }
}
?>